package com.example.fragment;

import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.adapter.MyHomeAdapter;
import com.example.adapter.MyQuestionAdapter;
import com.example.android.HomeTest;
import com.example.android.QuestionTest;
import com.example.androidtest.MyAdapter;
import com.example.androidtest.MyThread;
import com.example.androidtest.R;
import com.example.androidtest.R.layout;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class Home_Fragment extends Fragment {
	private ArrayList<HomeTest> list;
	private MyHomeAdapter adapter;
	private ViewPager viewPager;
	private String result;
	private Handler handler;
	private String url = "http://www.mengxianyi.net/one/homepage.json";
	private ImageButton btn_im;
	private TextView textView_zan;
	private static int hometime = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_home, null);
		viewPager = (ViewPager) view.findViewById(R.id.vp_home);
		btn_im = (ImageButton) view.findViewById(R.id.btn_im);
		textView_zan = (TextView) view.findViewById(R.id.tv_home_zan);
		btn_im.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				hometime++;
				textView_zan.setText("" + hometime);
			}
		});
		handler = new Handler(new Handler.Callback() {

			@Override
			public boolean handleMessage(Message msg) {
				if (msg.what == 100) {
					result = (String) msg.obj;
					list = getData();
					if (list.size() > 0) {

						adapter = new MyHomeAdapter(getActivity(), list);
						viewPager.setAdapter(adapter);
					}
				}

				return false;
			}
		});

		new MyThread(handler, url).start();
		return view;
	}

	public ArrayList<HomeTest> getData() {
		ArrayList<HomeTest> data = new ArrayList<>();
		try {
			JSONObject object = new JSONObject(result);
			if (object.has("data")) {
				JSONArray array = object.getJSONArray("data");
				for (int i = 0; i < array.length(); i++) {
					HomeTest homeTest = new HomeTest();
					JSONObject o = array.getJSONObject(i);
					if (o.has("title")) {
						String title = o.getString("title");
						homeTest.setTitle(title);
					}
					if (o.has("author")) {
						String author = o.getString("author");
						homeTest.setQuthor(author);
					}
					if (o.has("content")) {
						String content = o.getString("content");
						homeTest.setContent(content);
					}
					if (o.has("image")) {
						String image = o.getString("image");
						homeTest.setImage(image);
					}
					data.add(homeTest);

				}
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return data;
	}
}
